using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Administration
{
    public class MembershipAppListPage : StarznetBaseAdministrationPage
    {
        protected WebApplicationSelector Selector;
        private DataView _oView;
        protected SPGridView GvFormsAuthApps;
        private MenuTemplate _oActionMenu;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _oActionMenu = new MenuTemplate();
            _oActionMenu.ID = "ActionMenu";

            SPMenuField oMenu = new SPMenuField();
            oMenu.HeaderText = this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_WebAppColHeader").ToString();
            oMenu.TextFields = "WebSiteName";
            oMenu.MenuTemplateId = "ActionMenu";
            oMenu.NavigateUrlFields = "WebSiteName";
            oMenu.NavigateUrlFormat = "ManageMembership.aspx?wapp={0}";
            oMenu.TokenNameAndValueFields = "NAME=WebSiteName,URL=URL";
            oMenu.SortExpression = "WebSiteName";

            MenuItemTemplate oMemberManage = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_MenuMgeMember").ToString(), "/_layouts/images/EDITITEM.GIF");
            oMemberManage.ClientOnClickNavigateUrl = "ManageMembership.aspx?wapp=%NAME%&url=%URL%";
            _oActionMenu.Controls.Add(oMemberManage);

            MenuItemTemplate oRoleManage = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_MenuMgeRole").ToString(), "/_layouts/images/EDITITEM.GIF");
            oRoleManage.ClientOnClickNavigateUrl = "ManageRoles.aspx?wapp=%NAME%&url=%URL%";
            _oActionMenu.Controls.Add(oRoleManage);

            //MenuItemTemplate oUserManage = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_MenuMgeUsers").ToString(), "/_layouts/images/CHNGCOL.GIF");
            //oUserManage.ClientOnClickNavigateUrl = "MembershipUserList.aspx?wapp=%NAME%&url=%URL%";
            //_oActionMenu.Controls.Add(oUserManage);

            //MenuItemTemplate oRoleList = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_MenuRolesList").ToString(), "/_layouts/images/EDITITEM.GIF");
            //oRoleList.ClientOnClickNavigateUrl = "MembershipRoleList.aspx?wapp=%NAME%&url=%URL%";
            //_oActionMenu.Controls.Add(oRoleList);

            MenuItemTemplate oCreateAdmin = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_MenuCreateAdmin").ToString(), "/_layouts/images/PERUSRCK.GIF");
            oCreateAdmin.ClientOnClickNavigateUrl = "CreateFAAdministrator.aspx?wapp=%NAME%&url=%URL%";
            _oActionMenu.Controls.Add(oCreateAdmin);

            this.Page.Controls.Add(_oActionMenu);
            GvFormsAuthApps.Columns.Add(oMenu);

            BoundField oDefaultProviderField = new BoundField();
            oDefaultProviderField.DataField = "DefaultProvider";
            oDefaultProviderField.HeaderText = this.GetGlobalResourceObject("szadm", "ManageFormsAuth_AppList_ProviderColHeader").ToString();
            oDefaultProviderField.SortExpression = "DefaultProvider";
            GvFormsAuthApps.Columns.Add(oDefaultProviderField);
        }

        public void OnContextChange(object sender, EventArgs e)
        {
            DataTable oDT = new DataTable();
            oDT.Columns.Add("WebSiteName");
            oDT.Columns.Add("DefaultProvider");
            oDT.Columns.Add("URL");

            String sWebApp = Selector.CurrentItem.Name;

            //Find all forms authenticated web application
            foreach (SPAlternateUrl oURL in Selector.CurrentItem.AlternateUrls)
            {
                try
                {
                    if (Selector.CurrentItem.IisSettings[oURL.UrlZone].AuthenticationMode == AuthenticationMode.Forms)
                    {
                        DataRow oRow = oDT.NewRow();
                        oRow["WebSiteName"] = Selector.CurrentItem.IisSettings[oURL.UrlZone].ServerComment;
                        oRow["DefaultProvider"] = Selector.CurrentItem.IisSettings[oURL.UrlZone].MembershipProvider;
                        oRow["URL"] = oURL.Uri.OriginalString;
                        oDT.Rows.Add(oRow);
                    }
                }
                catch (KeyNotFoundException)
                {
                    //Ignore this exception - likely we just have an alternate access mapping which
                    //is not neccessarily an extended web application
                }
            }
            
            _oView = new DataView(oDT);

            GvFormsAuthApps.DataSource = _oView;
            GvFormsAuthApps.DataBind();
        }

    }
}
